package com.help10000.rms.common

import android.app.Activity
import android.app.Application
import android.os.Bundle

/**
 * @author justkeepmoving.
 * @date 2019/6/10 17:02.
 * make your program better than last time.
 */
class TopActivityHelper {

    fun register(application: Application) {
        application.registerActivityLifecycleCallbacks(object :
            Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                topActivity = activity

            }

            override fun onActivityStarted(activity: Activity) {
                topActivity = activity
                counter++
            }

            override fun onActivityResumed(activity: Activity) {
                topActivity = activity

            }

            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {
                counter--

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {}
        })
    }

    companion object {
        var topActivity: Activity? = null

        private var counter = 0
        val isAppInFront: Boolean
            get() = counter > 0


    }
}